package com.woorigil.social;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.woorigil.social.domain.category.Category;

public class Context {
	public static Date lastUpdatedAt;
	public static int totalDealCount;
	
	public static Map<String, Category> mainCategories = new HashMap<String, Category>();
	private static Map<Integer, Map<String, Category>> subCategories = new HashMap<Integer, Map<String, Category>>();
	
	public static List<MenuCategory> menuCategories = new ArrayList<MenuCategory>();
	public static List<Category> allCategories = new ArrayList<Category>();
	
	private static ThreadLocal<Data> local = new ThreadLocal<Data>();

	public static Data get() {
		Data data = local.get();
		if (data == null) {
			return new Data();
		}
		return data;
	}
	
	public static String getNavigationPath() {
		MenuCategory mainMenu = getMainCategory(get().mainMenuId);
		
		if (mainMenu.getCategory() == null) {
			return "쿠폰플레이";
		}
		
		String mainMenuName = mainMenu.getCategory().getName();
		String subMenuName = null;
		
		for (Category subCategory : mainMenu.getSubCategories()) {
			if (subCategory.getId() == get().subMenuId) {
				subMenuName = subCategory.getName();
			}
		}

		if (StringUtils.isBlank(mainMenuName)) {
			return "카테고리";
		} else {
			if (StringUtils.isBlank(subMenuName)) {
				return mainMenuName;
			} else {
				return mainMenuName + " > " + subMenuName;
			}
		}
	}	
	
	public static void addMainMenu(MenuCategory mainMenu) {
		if (menuCategories == null) {
			menuCategories = new ArrayList<MenuCategory>();
		}
		menuCategories.add(mainMenu);
	}
	
	public static MenuCategory getMainCategory(int mainMenuId) {
		for (MenuCategory each : menuCategories) {
			if (each.getCategory().getId() == mainMenuId) {
				return each;
			}
		}
		return new MenuCategory();
	}	
	
//	public static void addCategory(String name, Category category) {
//		if (categoryMap == null) {
//			categoryMap = new HashMap<String, Category>();
//		}
//		categoryMap.put(name, category);
//	}
//	
//	public static int getCategoryId(String categoryName) {
//		Category category = categoryMap.get(categoryName);
//		if (category == null) {
//			return 0;
//		}
//		return category.getId();
//	}

	public static void set(Data data) {
		local.set(data);
	}

	public static void remove() {
		local.remove();
	}
	
	public static Date getLastUpdatedAt() {
		return lastUpdatedAt;
	}

	public static void setLastUpdatedAt(Date lastUpdatedAt) {
		Context.lastUpdatedAt = lastUpdatedAt;
	}

	public static int getTotalDealCount() {
		return totalDealCount;
	}

	public static void setTotalDealCount(int totalDealCount) {
		Context.totalDealCount = totalDealCount;
	}

//	public static Map<String, Category> getCategoryMap() {
//		return categoryMap;
//	}
//
//	public static void setCategoryMap(Map<String, Category> categories) {
//		Context.categoryMap = categories;
//	}

	public static List<MenuCategory> getMainMenus() {
		return menuCategories;
	}

	public static List<Category> getFirstCategories() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public static List<Integer> getSubCategoryIds() {
		if (Context.get().getSubMenuId() != 0) {
			return Arrays.asList(Context.get().getSubMenuId());
		} else if (Context.get().getMainMenuId() != 0) {
			MenuCategory mainMenu = Context.getMainCategory(Context.get().getMainMenuId());
			return mainMenu.getSubCategoryIds();
		}
		return new ArrayList<Integer>();
	}
	
	public static List<Integer> getSubCategoryIds(int mainCategoryId, int subCategoryId) {
		if (subCategoryId != 0) {
			return Arrays.asList(subCategoryId);
		} else if (mainCategoryId != 0) {
			MenuCategory mainCategory = Context.getMainCategory(mainCategoryId);
			return mainCategory.getSubCategoryIds();
		}
		return new ArrayList<Integer>();
	}

	public static int getMainCategoryId(String key) {
		return mainCategories.get(key).getId();
	}
	
	public static int getSubCategoryId(int mainCategoryId, String subCategoryName) {
		Map<String, Category> subCategoryMap = subCategories.get(mainCategoryId);
		Category category = subCategoryMap.get(subCategoryName);
		if (category == null) {
			return 0;
		}
		return category.getId();
	}	

	public static void addMainCategory(String name, Category mainCategory) {
		mainCategories.put(name, mainCategory);
	}

	public static void addSubCategoryMap(Category subCategory) {
		Map<String, Category> subcategories = subCategories.get(subCategory.getParentId());
		if (subcategories == null) {
			subcategories = new HashMap<String, Category>();
		}
		subcategories.put(subCategory.getName(), subCategory);
		subCategories.put(subCategory.getParentId(), subcategories);
	}
	
	public static class Data {
		private boolean isMobileDevice;
		private String layout;
		private String extention;
		private int mainMenuId;
		private int subMenuId;
		private String userAgent;
		private String remoteAddr;

		public boolean isMobileDevice() {
			return isMobileDevice;
		}

		public void setMobileDevice(boolean isMobileDevice) {
			this.isMobileDevice = isMobileDevice;
		}

		public String getLayout() {
			return StringUtils.trimToEmpty(layout);
		}

		public void setLayout(String layout) {
			this.layout = layout;
		}

		public String getExtention() {
			return extention;
		}

		public void setExtention(String extention) {
			this.extention = extention;
		}

		public String getUserAgent() {
			return userAgent;
		}

		public void setUserAgent(String userAgent) {
			this.userAgent = userAgent;
		}

		public String getRemoteAddr() {
			return remoteAddr;
		}

		public void setRemoteAddr(String remoteAddr) {
			this.remoteAddr = remoteAddr;
		}

		public int getMainMenuId() {
			return mainMenuId;
		}

		public void setMainMenuId(int mainMenuId) {
			this.mainMenuId = mainMenuId;
		}

		public int getSubMenuId() {
			return subMenuId;
		}

		public void setSubMenuId(int subMenuId) {
			this.subMenuId = subMenuId;
		}
	}	
}
